package top.infopub.job.hr.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.job.hr.dao.*;
import top.infopub.job.hr.service.INCtoPMService;
import top.infopub.job.util.DateUtils;
import top.infopub.job.util.DeptUtil;
import top.infopub.job.util.NCUtil;
import top.infopub.model.hr.*;
import top.infopub.model.sys.Operator;
import top.infopub.model.sys.Operatorrole;
import top.infopub.model.sys.Role;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;
import top.infopub.util.MD5;
import top.infopub.util.StringHelper;

import java.text.SimpleDateFormat;
import java.util.*;

import static top.infopub.job.util.DeptUtil.getRoleNameByDept;
import static top.infopub.job.util.DeptUtil.pmOrg;

@Service
public class NCtoPmServiceImpl implements INCtoPMService {

    @Autowired
    private NcOrginfoDao ncOrginfoDao;

    @Autowired
    private NcStaffinfoDao ncStaffinfoDao;
    @Autowired
    private PMStaffInfoDao pmStaffInfoDao;
    @Autowired
    private PMOperatorDao pmOperatorDao;
    @Autowired
    private PMStaffOfficeDao pmStaffOfficeDao;
    @Autowired
    private PMDeptInfoDao pmDeptInfoDao;
    @Autowired
    private PMPostInfoDao pmPostInfoDao;
    @Autowired
    private PMRoleDao pmRoleDao;
    @Autowired
    private PMOperatorroleDao pmOperatorroleDao;
    @Autowired
    private OrgInfoDao orgInfoDao;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;


    @Override
    @Transactional
    public void updateData() {
        try {
            Date date=new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, -1);  // 在当前日基础上-1
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            String startdate=format.format(calendar.getTime())+" 00:00:00";
            List<NcOrginfo> ncOrginfos = ncOrginfoDao.selectByDay(startdate);
            //List<NcOrginfo> ncOrginfos = ncOrginfoDao.selectNcOrginfoList(new NcOrginfo());
            for (int i = 0; i <ncOrginfos.size() ; i++) {
                NcOrginfo ncOrginfo = ncOrginfos.get(i);
                if("Z".equalsIgnoreCase(ncOrginfo.getBzflag())){
                    OrgInfo orgInfo = orgInfoDao.selectByPkOrg(ncOrginfo.getPkOrg());
                    if(orgInfo!=null){
                        BeanUtils.copyProperties(ncOrginfo,orgInfo);
                        orgInfo.setParentId(ncOrginfo.getPkFatherorg());
                        orgInfo.setCompName(ncOrginfo.getName());
                        orgInfo.setLegalPerson(ncOrginfo.getFzrname());
                        orgInfo.setFzrid(ncOrginfo.getFzrid());
                        orgInfo.setFzrcode(ncOrginfo.getFzrcode());
                        orgInfo.setFzrname(ncOrginfo.getFzrname());
                        orgInfo.setFgid(ncOrginfo.getFgid());
                        orgInfo.setFgcode(ncOrginfo.getFgcode());
                        orgInfo.setFgname(ncOrginfo.getFgname());
                        if("Y".equals(ncOrginfo.getIseffect())){
                            orgInfo.setState(1);
                        }else {
                            orgInfo.setState(0);
                        }
                        orgInfo.setAddBy("job");
                        orgInfo.setAddTime(DateUtils.getCurrentDateTime());
                        orgInfo.setUptBy("job");
                        orgInfo.setUptTime(DateUtils.getCurrentDateTime());
                        orgInfoDao.updateByPrimaryKey(orgInfo);
                    }else{
                        orgInfo=new OrgInfo();
                        BeanUtils.copyProperties(ncOrginfo,orgInfo);
                        orgInfo.setId(ncOrginfo.getPkOrg());
                        orgInfo.setParentId(ncOrginfo.getPkFatherorg());
                        orgInfo.setCompName(ncOrginfo.getName());
                        orgInfo.setOrgCode(ncOrginfo.getCode());
                        orgInfo.setLegalPerson(ncOrginfo.getFzrname());
                        orgInfo.setFzrid(ncOrginfo.getFzrid());
                        orgInfo.setFzrcode(ncOrginfo.getFzrcode());
                        orgInfo.setFzrname(ncOrginfo.getFzrname());
                        orgInfo.setFgid(ncOrginfo.getFgid());
                        orgInfo.setFgcode(ncOrginfo.getFgcode());
                        orgInfo.setFgname(ncOrginfo.getFgname());
                        if("Y".equals(ncOrginfo.getIseffect())){
                            orgInfo.setState(1);
                        }else {
                            orgInfo.setState(0);
                        }
                        if(ncOrginfo.getName().indexOf("大建筑集团")>-1||ncOrginfo.getName().indexOf("江苏中南建设集团上海投资发展有限公司")>-1){
                            orgInfo.setTypes("1");
                        }else{
                            orgInfo.setTypes("3");
                        }

                        orgInfo.setAddBy("job");
                        orgInfo.setAddTime(DateUtils.getCurrentDateTime());
                        orgInfo.setUptBy("job");
                        orgInfo.setUptTime(DateUtils.getCurrentDateTime());

                        if(pmOrg.containsKey(ncOrginfo.getCode())){
                            OrgInfo oldOrgInfo = orgInfoDao.selectByOrgCode(pmOrg.get(ncOrginfo.getCode()));
                            if(oldOrgInfo!=null){
                                //orgInfo.setId(oldOrgInfo.getId());
                                //orgInfo.setOrgCode(oldOrgInfo.getOrgCode());
                                if("2".equals(oldOrgInfo.getTypes())){
                                    orgInfo.setTypes("2");
                                }
                            }
                        }
                        orgInfoDao.insert(orgInfo);
                    }

                }
                if("B".equalsIgnoreCase(ncOrginfo.getBzflag())){
                    DeptInfo deptInfo = pmDeptInfoDao.selectByPrimaryKey(ncOrginfo.getPkOrg());
                    if(deptInfo!=null){
                        deptInfo.setDeptName(ncOrginfo.getName());
                        deptInfo.setDeptCode(ncOrginfo.getCode());
                        deptInfo.setDeptLeader(ncOrginfo.getFgname());
                        deptInfo.setFzrid(ncOrginfo.getFzrid());
                        deptInfo.setFzrcode(ncOrginfo.getFzrcode());
                        deptInfo.setFzrname(ncOrginfo.getFzrname());
                        deptInfo.setFgid(ncOrginfo.getFgid());
                        deptInfo.setFgcode(ncOrginfo.getFgcode());
                        deptInfo.setFgname(ncOrginfo.getFgname());
                        if("Y".equals(ncOrginfo.getIseffect())){
                            deptInfo.setState(1);// 默认是1=启用
                        }else{
                            deptInfo.setState(0);
                        }
                        deptInfo.setUptBy("job");
                        deptInfo.setUptTime(DateUtils.getCurrentDateTime());
                        pmDeptInfoDao.updateByPrimaryKey(deptInfo);
                    }else{
                        OrgInfo orgInfo = orgInfoDao.selectByPkOrg(ncOrginfo.getBmsspkOrg());
                        deptInfo = new DeptInfo();
                        NcOrginfo ncDeptinfo = ncOrginfo;
                        deptInfo.setId(ncDeptinfo.getPkOrg());
                        deptInfo.setOrgId(orgInfo!=null?orgInfo.getId():ncOrginfo.getBmsspkOrg());
                        deptInfo.setDeptName(ncDeptinfo.getName());
                        deptInfo.setDeptCode(ncDeptinfo.getCode());
                        deptInfo.setDeptLeader(ncDeptinfo.getFgname());
                        deptInfo.setDeptMaxNum(300);
                        deptInfo.setParentId("-1");
                        if("Y".equals(ncDeptinfo.getIseffect())){
                            deptInfo.setState(1);// 默认是1=启用
                        }else{
                            deptInfo.setState(0);
                        }
                        deptInfo.setAddBy("job");
                        deptInfo.setAddTime(DateUtils.getCurrentDateTime());
                        deptInfo.setUptBy("job");
                        deptInfo.setUptTime(DateUtils.getCurrentDateTime());
                        deptInfo.setLevelIdx(1);
                        deptInfo.setPkDept(ncDeptinfo.getPkOrg());
                        deptInfo.setFzrid(ncOrginfo.getFzrid());
                        deptInfo.setFzrcode(ncOrginfo.getFzrcode());
                        deptInfo.setFzrname(ncOrginfo.getFzrname());
                        deptInfo.setFgid(ncOrginfo.getFgid());
                        deptInfo.setFgcode(ncOrginfo.getFgcode());
                        deptInfo.setFgname(ncOrginfo.getFgname());
                        pmDeptInfoDao.insertById(deptInfo);
                    }
                }
            }

            NcStaffinfo nc=new NcStaffinfo();
            List<NcStaffinfo> ncStaffinfos = ncStaffinfoDao.selectNcStaffinfoList(nc);
            if(ncStaffinfos!=null&&ncStaffinfos.size()>0) {
                for (NcStaffinfo ncStaffinfo : ncStaffinfos) {
                    StaffInfo staffInfo = pmStaffInfoDao.selectByPk(ncStaffinfo.getPkPsndoc());
                    if(staffInfo==null){
                        staffInfo=new StaffInfo();
                        BeanUtils.copyProperties(ncStaffinfo,staffInfo);
                        //原有字段填充
                        staffInfo.setCode(ncStaffinfo.getCode());
                        staffInfo.setStaffName(ncStaffinfo.getName());
                        staffInfo.setPhone(ncStaffinfo.getMobile());
                        String staffId = ncStaffinfo.getPkPsndoc();//人员id
                        staffInfo.setId(staffId);
                        //String staffCode = codeGeneratorConfigService.generatorCode("hr_staff_info");
                        String staffCode=ncStaffinfo.getCode();
                        staffInfo.setStaffCode(staffCode);
                        if("Y".equals(ncStaffinfo.getIseffect())){
                            staffInfo.setState(1);
                        }else {
                            staffInfo.setState(0);
                        }
                        staffInfo.setUptTime(DateUtils.getCurrentDateTime());
                        staffInfo.setAddTime(DateUtils.getCurrentDateTime());
                        staffInfo.setAddBy("job");
                        staffInfo.setUptBy("job");
                        pmStaffInfoDao.insertById(staffInfo);
                        //新增系统人员表
                        Operator operator = new Operator();
                        operator.setSpassword("Znjt123456");
                        //operator.setSoperatorNo(codeGeneratorConfigService.generatorCode("sys_operator"));
                        operator.setSoperatorNo(ncStaffinfo.getCode());
                        operator.setDaddDate(new Date());
                        operator.setSaddOperator("job");
                        operator.setBisAdmin(0);
                        if("Y".equals(ncStaffinfo.getIseffect())){
                            operator.setBisFreeze(1);
                        }else {
                            operator.setBisFreeze(0);
                        }
                        operator.setSuserName(ncStaffinfo.getEmail()!=null?ncStaffinfo.getEmail().split("@")[0]:StringHelper.getFullSpell(ncStaffinfo.getName(),false));
                        operator.setSrealName(ncStaffinfo.getName());
                        operator.setSmobile(ncStaffinfo.getMobile());
                        operator.setHrStaffId(staffInfo.getId());
                        operator.setSpyName(StringHelper.getFullSpell(ncStaffinfo.getName()));
                        operator.setSjpName(StringHelper.getFirstSpell(ncStaffinfo.getName()));
                        // MD加盐MD5.salt(operator.getSuserName(), pasword)
                        //operator.setSpassword(MD5.salt(operator.getSuserName(), operator.getSpassword()));
                        operator.setSpassword(MD5.encode(operator.getSuserName() + "**X7&2K$O@7XH" + operator.getSpassword()));
                        operator.setBisDelete(0);
                        pmOperatorDao.insert(operator);

                        OrgInfo orgInfo = orgInfoDao.selectByPkOrg(ncStaffinfo.getPkOrg());
                        if(orgInfo==null){
                            JSONObject sr = NCUtil.getOrgInfo("1","500",ncStaffinfo.getPkOrg(),null,null);
                            JSONArray data = sr.getJSONArray("data");
                            if(data!=null&&data.size()>0) {
                                List<NcOrginfo> list = JSONArray.parseArray(data.toString(), NcOrginfo.class);
                                for (NcOrginfo ncOrginfo : list) {
                                    orgInfo=new OrgInfo();
                                    BeanUtils.copyProperties(ncOrginfo,orgInfo);
                                    orgInfo.setId(ncOrginfo.getPkOrg());
                                    orgInfo.setParentId(ncOrginfo.getPkFatherorg());
                                    orgInfo.setCompName(ncOrginfo.getName());
                                    orgInfo.setOrgCode(ncOrginfo.getCode());
                                    orgInfo.setLegalPerson(ncOrginfo.getFzrname());
                                    orgInfo.setState(1);// 默认是1=启用
                                    if(ncOrginfo.getName().indexOf("大建筑集团")>-1||ncOrginfo.getName().indexOf("江苏中南建设集团上海投资发展有限公司")>-1){
                                        orgInfo.setTypes("1");
                                    }else{
                                        orgInfo.setTypes("3");
                                    }
                                    orgInfo.setAddBy("job");
                                    orgInfo.setAddTime(DateUtils.getCurrentDateTime());
                                    orgInfo.setUptBy("job");
                                    orgInfo.setUptTime(DateUtils.getCurrentDateTime());
                                    orgInfoDao.insert(orgInfo);
                                }
                            }
                        }
                        //新增部门信息
                        DeptInfo deptInfo = pmDeptInfoDao.selectByPrimaryKey(ncStaffinfo.getPkDept());
                        if(deptInfo==null){
                            JSONObject sr = NCUtil.getOrgInfo("1","500",ncStaffinfo.getPkDept(),null,null);
                            JSONArray data = sr.getJSONArray("data");
                            if(data!=null&&data.size()>0) {
                                List<NcOrginfo> list = JSONArray.parseArray(data.toString(), NcOrginfo.class);
                                for (NcOrginfo ncOrginfo : list) {
                                    deptInfo = new DeptInfo();
                                    NcOrginfo ncDeptinfo = ncOrginfo;
                                    deptInfo.setId(ncDeptinfo.getPkOrg());
                                    deptInfo.setOrgId(orgInfo!=null?orgInfo.getId():ncOrginfo.getBmsspkOrg());
                                    deptInfo.setDeptName(ncDeptinfo.getName());
                                    deptInfo.setDeptCode(ncDeptinfo.getCode());
                                    deptInfo.setDeptLeader(ncDeptinfo.getFgname());
                                    deptInfo.setDeptMaxNum(300);
                                    deptInfo.setParentId("-1");
                                    deptInfo.setState(1);// 默认是1=启用
                                    deptInfo.setAddBy("job");
                                    deptInfo.setAddTime(DateUtils.getCurrentDateTime());
                                    deptInfo.setUptBy("job");
                                    deptInfo.setUptTime(DateUtils.getCurrentDateTime());
                                    deptInfo.setLevelIdx(1);
                                    deptInfo.setPkDept(ncDeptinfo.getPkOrg());
                                    pmDeptInfoDao.insertById(deptInfo);
                                }
                            }
                        }

                        //新增岗位信息
                        PostInfo postInfo = pmPostInfoDao.selectByPrimaryKey(ncStaffinfo.getPkPost());
                        if(postInfo==null&&StringUtils.isNotEmpty(ncStaffinfo.getPkPost())){
                            //新增岗位信息
                            postInfo=new PostInfo();
                            postInfo.setId(ncStaffinfo.getPkPost());
                            postInfo.setOrgId(orgInfo.getId());
                            postInfo.setDeptId(ncStaffinfo.getPkDept());
                            postInfo.setPostCode(ncStaffinfo.getPostcode());
                            postInfo.setPostName(ncStaffinfo.getPostname());
                            postInfo.setState(1);
                            postInfo.setAddBy("job");
                            postInfo.setAddTime(DateUtils.getCurrentDateTime());
                            postInfo.setUptBy("job");
                            postInfo.setUptTime(DateUtils.getCurrentDateTime());
                            pmPostInfoDao.insert(postInfo);
                        }
                        //新增就职记录

                        StaffOffice staffOffice =new StaffOffice();
                        staffOffice.setStaffId(staffId);
                        staffOffice.setOrgId(orgInfo.getId());
                        if(deptInfo!=null){
                            staffOffice.setDeptId(deptInfo.getId());
                        }
                        if(postInfo!=null){
                            staffOffice.setPostId(postInfo.getId());
                        }
                        staffOffice.setShowIdx(1);
                        staffOffice.setStartTime(DateUtils.getCurrentDateTime());
                        staffOffice.setAddBy("job");
                        staffOffice.setAddTime(DateUtils.getCurrentDateTime());
                        staffOffice.setUptBy("job");
                        staffOffice.setUptTime(DateUtils.getCurrentDateTime());
                        pmStaffOfficeDao.insert(staffOffice);
                        //关联角色
                        Role role = pmRoleDao.selectByDomainWhere(getRoleNameByDept(deptInfo!=null?deptInfo.getDeptName():ncStaffinfo.getDeptName()));
                        Operatorrole operatorRole = new Operatorrole();
                        operatorRole.setSoperatorId(operator.getId());
                        operatorRole.setSroleId(role.getId());
                        pmOperatorroleDao.insert(operatorRole);
                    }else{
                        //PM系统有这个人员，更新人员信息
                        BeanUtils.copyProperties(ncStaffinfo,staffInfo);
                        staffInfo.setCode(ncStaffinfo.getCode());
                        staffInfo.setUptTime(DateUtils.getCurrentDateTime());
                        staffInfo.setAddTime(DateUtils.getCurrentDateTime());
                        staffInfo.setPhone(ncStaffinfo.getMobile());
                        if("Y".equals(ncStaffinfo.getIseffect())){
                            staffInfo.setState(1);
                        }else {
                            staffInfo.setState(0);
                        }
                        staffInfo.setAddBy("job");
                        staffInfo.setUptBy("job");
                        pmStaffInfoDao.updateByPrimaryKey(staffInfo);

                        Operator operator=new Operator();
                        operator.setHrStaffId(staffInfo.getId());
                        List<Operator> operators = pmOperatorDao.selectByEntityWhere(operator);
                        if(operators!=null&&operators.size()>0){
                            operator=operators.get(0);
                            if("Y".equals(ncStaffinfo.getIseffect())){
                                operator.setBisFreeze(1);
                            }else {
                                operator.setBisFreeze(0);
                            }
                            pmOperatorDao.updateByPrimaryKey(operator);
                        }


                        //查询就职信息（组织、部门、岗位）
                        List<StaffOffice> staffOffices = pmStaffOfficeDao.selectStaffOfficeByUserId(staffInfo.getId());
                        if(staffOffices!=null&&staffOffices.size()>0) {
                            for (int i = 0; i < staffOffices.size(); i++) {
                                StaffOffice staffOffice = staffOffices.get(i);
                                if (i > 0) {
                                    continue;
                                } else {
                                    pmStaffOfficeDao.deleteByPrimaryKey(staffOffice.getId());

                                    OrgInfo orgInfo = orgInfoDao.selectByPrimaryKey(staffOffice.getOrgId());

                                    if(orgInfo==null){
                                        JSONObject sr = NCUtil.getOrgInfo("1","500",ncStaffinfo.getPkOrg(),null,null);
                                        JSONArray data = sr.getJSONArray("data");
                                        if(data!=null&&data.size()>0) {
                                            List<NcOrginfo> list = JSONArray.parseArray(data.toString(), NcOrginfo.class);
                                            for (NcOrginfo ncOrginfo : list) {
                                                orgInfo=new OrgInfo();
                                                BeanUtils.copyProperties(ncOrginfo,orgInfo);
                                                orgInfo.setId(ncOrginfo.getPkOrg());
                                                orgInfo.setParentId(ncOrginfo.getPkFatherorg());
                                                orgInfo.setCompName(ncOrginfo.getName());
                                                orgInfo.setOrgCode(ncOrginfo.getCode());
                                                orgInfo.setLegalPerson(ncOrginfo.getFzrname());
                                                orgInfo.setState(1);// 默认是1=启用
                                                if(ncOrginfo.getName().indexOf("大建筑集团")>-1||ncOrginfo.getName().indexOf("江苏中南建设集团上海投资发展有限公司")>-1){
                                                    orgInfo.setTypes("1");
                                                }else{
                                                    orgInfo.setTypes("3");
                                                }
                                                orgInfo.setAddBy("job");
                                                orgInfo.setAddTime(DateUtils.getCurrentDateTime());
                                                orgInfo.setUptBy("job");
                                                orgInfo.setUptTime(DateUtils.getCurrentDateTime());
                                                orgInfoDao.insert(orgInfo);
                                            }
                                        }
                                    }
                                    //新增部门信息
                                    DeptInfo deptInfo = pmDeptInfoDao.selectByPrimaryKey(ncStaffinfo.getPkDept());
                                    if(deptInfo==null){
                                        JSONObject sr = NCUtil.getOrgInfo("1","500",ncStaffinfo.getPkDept(),null,null);
                                        JSONArray data = sr.getJSONArray("data");
                                        if(data!=null&&data.size()>0) {
                                            List<NcOrginfo> list = JSONArray.parseArray(data.toString(), NcOrginfo.class);
                                            for (NcOrginfo ncOrginfo : list) {
                                                deptInfo = new DeptInfo();
                                                NcOrginfo ncDeptinfo = ncOrginfo;
                                                deptInfo.setId(ncDeptinfo.getPkOrg());
                                                deptInfo.setOrgId(orgInfo!=null?orgInfo.getId():ncOrginfo.getBmsspkOrg());
                                                deptInfo.setDeptName(ncDeptinfo.getName());
                                                deptInfo.setDeptCode(ncDeptinfo.getCode());
                                                deptInfo.setDeptLeader(ncDeptinfo.getFgname());
                                                deptInfo.setDeptMaxNum(300);
                                                deptInfo.setParentId("-1");
                                                deptInfo.setState(1);// 默认是1=启用
                                                deptInfo.setAddBy("job");
                                                deptInfo.setAddTime(DateUtils.getCurrentDateTime());
                                                deptInfo.setUptBy("job");
                                                deptInfo.setUptTime(DateUtils.getCurrentDateTime());
                                                deptInfo.setLevelIdx(1);
                                                deptInfo.setPkDept(ncDeptinfo.getPkOrg());
                                                pmDeptInfoDao.insertById(deptInfo);
                                            }
                                        }
                                    }

                                    PostInfo postInfo = pmPostInfoDao.selectByPrimaryKey(ncStaffinfo.getPkPost());
                                    if (postInfo == null) {
                                        //新增岗位信息
                                        postInfo = new PostInfo();
                                        postInfo.setId(ncStaffinfo.getPkPost());
                                        postInfo.setOrgId(orgInfo.getId());
                                        postInfo.setDeptId(ncStaffinfo.getPkDept());
                                        postInfo.setPostCode(ncStaffinfo.getPostcode());
                                        postInfo.setPostName(ncStaffinfo.getPostname());
                                        postInfo.setState(1);
                                        postInfo.setAddBy("job");
                                        postInfo.setAddTime(DateUtils.getCurrentDateTime());
                                        postInfo.setUptBy("job");
                                        postInfo.setUptTime(DateUtils.getCurrentDateTime());
                                        pmPostInfoDao.insert(postInfo);
                                    }
                                    //新增就职记录
                                    staffOffice = new StaffOffice();
                                    staffOffice.setStaffId(staffInfo.getId());
                                    staffOffice.setOrgId(orgInfo.getId());
                                    staffOffice.setDeptId(deptInfo.getId());
                                    staffOffice.setPostId(postInfo.getId());
                                    staffOffice.setShowIdx(1);
                                    staffOffice.setStartTime(DateUtils.getCurrentDateTime());
                                    staffOffice.setAddBy("job");
                                    staffOffice.setAddTime(DateUtils.getCurrentDateTime());
                                    staffOffice.setUptBy("job");
                                    staffOffice.setUptTime(DateUtils.getCurrentDateTime());
                                    pmStaffOfficeDao.insert(staffOffice);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    @Transactional
    public Map initOrgData20210106() {
        Map<String,String> orgpkMap=new HashMap<>();
        NcOrginfo nc=new NcOrginfo();
        nc.setBzflag("Z");
        List<NcOrginfo> ncOrginfos = ncOrginfoDao.selectNcOrginfoDistinctList(nc);
        List<OrgInfo> orgInfos=new ArrayList<>();
        for (int i = 0; i <ncOrginfos.size() ; i++) {
            NcOrginfo ncOrginfo = ncOrginfos.get(i);
            OrgInfo orgInfo=new OrgInfo();

            BeanUtils.copyProperties(ncOrginfo,orgInfo);
            orgInfo.setId(ncOrginfo.getPkOrg());
            orgInfo.setParentId(ncOrginfo.getPkFatherorg());
            orgInfo.setCompName(ncOrginfo.getName());
            orgInfo.setOrgCode(ncOrginfo.getCode());
            orgInfo.setLegalPerson(ncOrginfo.getFzrname());
            orgInfo.setState(1);// 默认是1=启用
            if(ncOrginfo.getName().indexOf("大建筑集团")>-1||ncOrginfo.getName().indexOf("江苏中南建设集团上海投资发展有限公司")>-1){
                orgInfo.setTypes("1");
            }else{
                orgInfo.setTypes("3");
            }

            orgInfo.setAddBy("job");
            orgInfo.setAddTime(DateUtils.getCurrentDateTime());
            orgInfo.setUptBy("job");
            orgInfo.setUptTime(DateUtils.getCurrentDateTime());

            if(pmOrg.containsKey(ncOrginfo.getCode())){
                OrgInfo oldOrgInfo = orgInfoDao.selectByOrgCode(pmOrg.get(ncOrginfo.getCode()));
                if(oldOrgInfo!=null){
                    orgpkMap.put(ncOrginfo.getPkOrg(),oldOrgInfo.getId());

                    orgInfo.setId(oldOrgInfo.getId());
                    orgInfo.setOrgCode(oldOrgInfo.getOrgCode());
                    if(StringUtils.isNotEmpty(oldOrgInfo.getProjectCode())){
                        orgInfo.setProjectCode(oldOrgInfo.getProjectCode());
                    }

                    //orgInfo.setParentId();
                    if("2".equals(oldOrgInfo.getTypes())){
                        orgInfo.setTypes("2");
                    }
                }

            }
            orgInfos.add(orgInfo);
        }
        orgInfoDao.deleteAll();

        int insertLength = orgInfos.size();
        int i = 0;
        while (insertLength > 500) {
            orgInfoDao.batchInsertOrginfo(orgInfos.subList(i, i + 500));
            i = i + 500;
            insertLength = insertLength - 500;
        }
        if (insertLength > 0) {
            orgInfoDao.batchInsertOrginfo(orgInfos.subList(i, i + insertLength));
        }


        nc.setBzflag("B");
        List<NcOrginfo> ncDeptInfos = ncOrginfoDao.selectNcOrginfoDistinctList(nc);
        List<DeptInfo> deptInfos=new ArrayList<>();
        for (int j = 0; j < ncDeptInfos.size(); j++) {
            NcOrginfo ncDeptinfo=ncDeptInfos.get(j);
            DeptInfo deptInfo=new DeptInfo();
            deptInfo.setId(ncDeptinfo.getPkOrg());
            if(orgpkMap.containsKey(ncDeptinfo.getBmsspkOrg())){
                deptInfo.setOrgId(orgpkMap.get(ncDeptinfo.getBmsspkOrg()));
            }else{
                deptInfo.setOrgId(ncDeptinfo.getBmsspkOrg());
            }
            deptInfo.setDeptName(ncDeptinfo.getName());
            deptInfo.setDeptCode(ncDeptinfo.getCode());
            deptInfo.setDeptLeader(ncDeptinfo.getFgname());
            deptInfo.setDeptMaxNum(300);
            deptInfo.setFzrid(ncDeptinfo.getFzrid());
            deptInfo.setFgcode(ncDeptinfo.getFzrcode());
            deptInfo.setFgname(ncDeptinfo.getFzrname());
            deptInfo.setFgid(ncDeptinfo.getFgid());
            deptInfo.setFgcode(ncDeptinfo.getFgcode());
            deptInfo.setFgname(ncDeptinfo.getFgname());
            if(ncDeptinfo.getBmsspkOrg().equals(ncDeptinfo.getPkFatherorg())){
                deptInfo.setParentId("-1");
                deptInfo.setLevelIdx(1);
            }else{
               // ncOrginfoDao.selectByPrimaryKey(ncDeptinfo.getPkFatherorg());
               deptInfo.setParentId(ncDeptinfo.getPkFatherorg());
               deptInfo.setLevelIdx(2);
            }
            deptInfo.setState(1);// 默认是1=启用
            deptInfo.setAddBy("job");
            deptInfo.setAddTime(DateUtils.getCurrentDateTime());
            deptInfo.setUptBy("job");
            deptInfo.setUptTime(DateUtils.getCurrentDateTime());

            deptInfo.setPkDept(ncDeptinfo.getPkOrg());
            deptInfos.add(deptInfo);
        }

        pmDeptInfoDao.deleteAll();
         insertLength = deptInfos.size();
         i = 0;
        while (insertLength > 500) {
            pmDeptInfoDao.insertByBatch(deptInfos.subList(i, i + 500));
            i = i + 500;
            insertLength = insertLength - 500;
        }
        if (insertLength > 0) {
            pmDeptInfoDao.insertByBatch(deptInfos.subList(i, i + insertLength));
        }
        return orgpkMap;
    }

    @Override
    @Transactional
    public void initUserData20210106(Map<String,String> map) {
        NcStaffinfo nc=new NcStaffinfo();
        List<NcStaffinfo> ncStaffinfos = ncStaffinfoDao.selectNcStaffinfoList(nc);
        if(ncStaffinfos!=null&&ncStaffinfos.size()>0) {
            for (NcStaffinfo ncStaffinfo : ncStaffinfos) {
                String ncMobile = ncStaffinfo.getMobile();
                //查询PM系统人员信息表
                List<StaffInfo> staffInfos = pmStaffInfoDao.selectByMobile(ncMobile);
                if(staffInfos==null||staffInfos.size()==0){
                    StaffInfo enty=new StaffInfo();
                    enty.setStaffName(ncStaffinfo.getName());
                    staffInfos = pmStaffInfoDao.selectByEntityWhere(enty);
                }
                if (staffInfos == null || staffInfos.size() == 0) {//PM系统没有这个人员需要新增
                    StaffInfo staffInfo=new StaffInfo();
                    BeanUtils.copyProperties(ncStaffinfo,staffInfo);
                    //原有字段填充
                    staffInfo.setStaffName(ncStaffinfo.getName());
                    staffInfo.setPhone(ncStaffinfo.getMobile());
                    String staffId = ncStaffinfo.getPkPsndoc();//人员id
                    staffInfo.setId(staffId);
                    //String staffCode = codeGeneratorConfigService.generatorCode("hr_staff_info");
                    String staffCode=ncStaffinfo.getCode();
                    staffInfo.setStaffCode(staffCode);
                    staffInfo.setState(1);//默认在职
                    staffInfo.setUptTime(DateUtils.getCurrentDateTime());
                    staffInfo.setAddTime(DateUtils.getCurrentDateTime());
                    staffInfo.setAddBy("job");
                    staffInfo.setUptBy("job");
                    pmStaffInfoDao.insertById(staffInfo);
                    //新增系统人员表
                    Operator operator = new Operator();
                    operator.setSpassword("Znjt123456");
                    operator.setSoperatorNo(codeGeneratorConfigService.generatorCode("sys_operator"));
                    operator.setDaddDate(new Date());
                    operator.setSaddOperator("job");
                    operator.setBisAdmin(0);
                    operator.setBisFreeze(1);
                    operator.setSuserName(ncStaffinfo.getEmail()!=null?ncStaffinfo.getEmail().split("@")[0]:StringHelper.getFullSpell(ncStaffinfo.getName(),false));
                    operator.setSrealName(ncStaffinfo.getName());
                    operator.setSmobile(ncStaffinfo.getMobile());
                    operator.setHrStaffId(staffInfo.getId());
                    operator.setSpyName(StringHelper.getFullSpell(ncStaffinfo.getName()));
                    operator.setSjpName(StringHelper.getFirstSpell(ncStaffinfo.getName()));
                    // MD加盐MD5.salt(operator.getSuserName(), pasword)
                    //operator.setSpassword(MD5.salt(operator.getSuserName(), operator.getSpassword()));
                    operator.setSpassword(MD5.encode(operator.getSuserName() + "**X7&2K$O@7XH" + operator.getSpassword()));
                    operator.setBisDelete(0);
                    pmOperatorDao.insert(operator);

                    OrgInfo orgInfo = orgInfoDao.selectByPkOrg(ncStaffinfo.getPkOrg());

                    //新增部门信息
                    DeptInfo deptInfo = pmDeptInfoDao.selectByPrimaryKey(ncStaffinfo.getPkDept());

                    //新增岗位信息
                    PostInfo postInfo = pmPostInfoDao.selectByPrimaryKey(ncStaffinfo.getPkPost());
                    if(postInfo==null){
                        //新增岗位信息
                        postInfo=new PostInfo();
                        postInfo.setId(ncStaffinfo.getPkPost());
                        postInfo.setOrgId(orgInfo.getId());
                        postInfo.setDeptId(ncStaffinfo.getPkDept());
                        postInfo.setPostCode(ncStaffinfo.getPostcode());
                        postInfo.setPostName(ncStaffinfo.getPostname());
                        postInfo.setState(1);
                        postInfo.setAddBy("job");
                        postInfo.setAddTime(DateUtils.getCurrentDateTime());
                        postInfo.setUptBy("job");
                        postInfo.setUptTime(DateUtils.getCurrentDateTime());
                        pmPostInfoDao.insert(postInfo);
                    }
                    //新增就职记录
                    StaffOffice staffOffice =new StaffOffice();
                    staffOffice.setStaffId(staffId);
                    staffOffice.setOrgId(orgInfo.getId());
                    staffOffice.setDeptId(deptInfo==null?ncStaffinfo.getPkDept():deptInfo.getId());
                    staffOffice.setPostId(postInfo.getId());
                    staffOffice.setShowIdx(1);
                    staffOffice.setStartTime(DateUtils.getCurrentDateTime());
                    staffOffice.setAddBy("job");
                    staffOffice.setAddTime(DateUtils.getCurrentDateTime());
                    staffOffice.setUptBy("job");
                    staffOffice.setUptTime(DateUtils.getCurrentDateTime());
                    pmStaffOfficeDao.insert(staffOffice);

                    //关联角色
                    Role role = pmRoleDao.selectByDomainWhere(getRoleNameByDept(deptInfo!=null?deptInfo.getDeptName():ncStaffinfo.getDeptName()));
                    Operatorrole operatorRole = new Operatorrole();
                    operatorRole.setSoperatorId(operator.getId());
                    operatorRole.setSroleId(role.getId());
                    pmOperatorroleDao.insert(operatorRole);

                }else{ //PM系统有这个人员，更新人员信息
                    StaffInfo staffInfo=staffInfos.get(0);
                    //PM系统有这个人员，更新人员信息
                    BeanUtils.copyProperties(ncStaffinfo,staffInfo);
                    staffInfo.setUptTime(DateUtils.getCurrentDateTime());
                    staffInfo.setAddTime(DateUtils.getCurrentDateTime());
                    staffInfo.setAddBy("job");
                    staffInfo.setUptBy("job");
                    pmStaffInfoDao.updateByPrimaryKey(staffInfo);

                    List<StaffOffice> staffOffices = pmStaffOfficeDao.selectStaffOfficeByUserId(staffInfo.getId());
                    if(staffOffices!=null&&staffOffices.size()>0) {
                        for (int i = 0; i < staffOffices.size(); i++) {
                            StaffOffice staffOffice = staffOffices.get(i);
                            pmStaffOfficeDao.deleteByPrimaryKey(staffOffice.getId());
                        }
                    }

                    String orgid="";
                    if(map.containsKey(ncStaffinfo.getPkOrg())){
                        orgid=map.get(ncStaffinfo.getPkOrg());
                    }else{
                        orgid=ncStaffinfo.getPkOrg();
                    }
                    OrgInfo orgInfo = orgInfoDao.selectByPrimaryKey(orgid);

                    DeptInfo deptInfo = pmDeptInfoDao.selectByPrimaryKey(ncStaffinfo.getPkDept());

                    PostInfo postInfo = pmPostInfoDao.selectByPrimaryKey(ncStaffinfo.getPkPost());
                    if (postInfo == null) {
                        //新增岗位信息
                        postInfo = new PostInfo();
                        postInfo.setId(ncStaffinfo.getPkPost());
                        postInfo.setOrgId(orgInfo.getId());
                        postInfo.setDeptId(ncStaffinfo.getPkDept());
                        postInfo.setPostCode(ncStaffinfo.getPostcode());
                        postInfo.setPostName(ncStaffinfo.getPostname());
                        postInfo.setState(1);
                        postInfo.setAddBy("job");
                        postInfo.setAddTime(DateUtils.getCurrentDateTime());
                        postInfo.setUptBy("job");
                        postInfo.setUptTime(DateUtils.getCurrentDateTime());
                        pmPostInfoDao.insert(postInfo);

                    }
                    //新增就职记录
                    StaffOffice staffOffice = new StaffOffice();
                    staffOffice.setStaffId(staffInfo.getId());
                    staffOffice.setOrgId(orgInfo.getId());
                    staffOffice.setDeptId(deptInfo.getId());
                    staffOffice.setPostId(postInfo.getId());
                    staffOffice.setShowIdx(1);
                    staffOffice.setStartTime(DateUtils.getCurrentDateTime());
                    staffOffice.setAddBy("job");
                    staffOffice.setAddTime(DateUtils.getCurrentDateTime());
                    staffOffice.setUptBy("job");
                    staffOffice.setUptTime(DateUtils.getCurrentDateTime());
                    pmStaffOfficeDao.insert(staffOffice);

                    Operator operator=new Operator();
                    operator.setHrStaffId(staffInfo.getId());
                    List<Operator> operators = pmOperatorDao.selectByEntityWhere(operator);
                    if(operators==null||operators.size()==0){
                        //新增系统人员表
                        operator = new Operator();
                        operator.setSpassword("Znjt123456");
                        operator.setSoperatorNo(codeGeneratorConfigService.generatorCode("sys_operator"));
                        operator.setDaddDate(new Date());
                        operator.setSaddOperator("job");
                        operator.setBisAdmin(0);
                        operator.setBisFreeze(1);
                        operator.setSuserName(ncStaffinfo.getEmail()!=null?ncStaffinfo.getEmail().split("@")[0]:StringHelper.getFullSpell(ncStaffinfo.getName(),false));
                        operator.setSrealName(ncStaffinfo.getName());
                        operator.setSmobile(ncStaffinfo.getMobile());
                        operator.setHrStaffId(staffInfo.getId());
                        operator.setSpyName(StringHelper.getFullSpell(ncStaffinfo.getName()));
                        operator.setSjpName(StringHelper.getFirstSpell(ncStaffinfo.getName()));
                        // MD加盐MD5.salt(operator.getSuserName(), pasword)
                        //operator.setSpassword(MD5.salt(operator.getSuserName(), operator.getSpassword()));
                        operator.setSpassword(MD5.encode(operator.getSuserName() + "**X7&2K$O@7XH" + operator.getSpassword()));
                        operator.setBisDelete(0);
                        pmOperatorDao.insert(operator);

                        //关联角色
                        Role role = pmRoleDao.selectByDomainWhere(getRoleNameByDept(deptInfo!=null?deptInfo.getDeptName():ncStaffinfo.getDeptName()));
                        Operatorrole operatorRole = new Operatorrole();
                        operatorRole.setSoperatorId(operator.getId());
                        operatorRole.setSroleId(role.getId());
                        pmOperatorroleDao.insert(operatorRole);
                    }

                }
            }
        }
    }
}
